package com.hu.common.util.excel;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.hu.common.util.excel.config.ExcelConfig;
import com.hu.common.util.excel.dto.PersonDTO;
import com.hu.common.util.excel.dto.PositionDTO;
import com.hu.common.util.excel.kit.ExcelKit;
import com.hu.common.util.excel.kit.ExcelSheetData;

import java.io.FileOutputStream;
import java.util.List;

/**
 * @author 00351634
 * @version 1.0
 * @date 2024/12/20 17:52
 * @description: TODO
 */
public class ExportTest {

    /** excel header下标 */
    public static final int HEADER_INDEX = 0;
    /** excel中头的行数，默认1行 */
    public static final int HEADER_ROW_NUMBER = 1;

    public static void main(String[] args) throws Exception {
        ExcelSheetData<PersonDTO> personComposite = new ExcelSheetData<>();
        personComposite.setSheetDefinition(PersonDTO.class);
        personComposite.setData(getPersonList());
        ExcelSheetData<PositionDTO> positionComposite = new ExcelSheetData<>();
        positionComposite.setSheetDefinition(PositionDTO.class);
        positionComposite.setData(getPositionList());
        List<ExcelSheetData> excelCompositeList = ListUtil.of(personComposite, positionComposite);
        exportData(excelCompositeList);
    }

    public static void exportData(List<ExcelSheetData> excelCompositeList) throws Exception {
        ExcelWriter writer = ExcelUtil.getWriter(true);
        for (ExcelSheetData excelComposite : excelCompositeList) {
            ExcelConfig.SheetConfig sheetConfig = ExcelConfig.getSheetConfig(excelComposite.getSheetDefinition());
            List<ExcelConfig.ColumnConfig> columnConfigList =
                ExcelConfig.getColumnConfig(excelComposite.getSheetDefinition());
            writer.setSheet(sheetConfig.getIndex());
            writer.renameSheet(sheetConfig.getIndex(), sheetConfig.getName());
            // 设置标题
            fillTitle(writer, columnConfigList);
            fillContent(writer, columnConfigList, excelComposite.getData());
        }
        doExport(writer);
    }

    public static void fillContent(ExcelWriter writer, List<ExcelConfig.ColumnConfig> columnConfigList,
        List contentList) {
        for (int j = 0; j < contentList.size(); j++) {
            for (int k = 0; k < columnConfigList.size(); k++) {
                Object fieldValue =
                    ReflectUtil.getFieldValue(contentList.get(j), columnConfigList.get(k).getFieldName());
                writer.writeCellValue(k, j + HEADER_ROW_NUMBER, fieldValue);
            }
        }
    }

    /**
     * 填充标题
     *
     * @param writer
     * @param columnConfigList
     */
    public static void fillTitle(ExcelWriter writer, List<ExcelConfig.ColumnConfig> columnConfigList) {
        for (int i = 0; i < columnConfigList.size(); i++) {
            writer.writeCellValue(i, HEADER_INDEX, columnConfigList.get(i).getHeader());
            ExcelKit.setCellComment(writer, i, HEADER_INDEX, columnConfigList.get(i).getNote());
        }
    }

    public static void doExport(ExcelWriter writer) throws Exception {
        FileOutputStream out = new FileOutputStream("D:\\test.xlsx");
        writer.flush(out);
        writer.close();
        IoUtil.close(out);
    }

    public static List<PersonDTO> getPersonList() {
        PersonDTO person1 = new PersonDTO();
        person1.setName("张三");
        person1.setAge(18);
        person1.setAddress("北京");
        person1.setStaffCode("001");
        person1.setType("A");
        PersonDTO person2 = new PersonDTO();
        person2.setName("李四");
        person2.setAge(19);
        person2.setAddress("上海");
        person2.setStaffCode("002");
        return ListUtil.of(person1, person2);
    }

    public static List<PositionDTO> getPositionList() {
        PositionDTO position1 = new PositionDTO();
        position1.setName("开发工程师");
        position1.setStaffCode("001");
        PositionDTO position2 = new PositionDTO();
        position2.setName("测试工程师");
        position2.setStaffCode("002");
        return ListUtil.of(position1, position2);
    }

}
